// pages/carpool/carpool.js

var Api = require('../../utils/api.js');
var wxRequest = require('../../utils/wxRequest.js');
var util = require('../../utils/util.js');
import config from '../../utils/config.js'
var app = getApp();
var pageCount = app.globalData.page_size ? app.globalData.page_size : 5;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentNav:0,
    carpoolList: [],
    page: 1,
    isLastPage: false,
    currentTypeId: 0,
    carpoolTypeList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    self.getCarCategries();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      carpoolList: [],
      isLastPage: false,
      page: 1
    });
    that.getCarpoolList(that.data.currentTypeId);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  //获取拼车分类
  getCarCategries: function () {
    var self = this;
    var getCarCategoriesRequest = wxRequest.getRequest(Api.getCarCategories());
    getCarCategoriesRequest.then(response => {
      if (response.statusCode == 200 && response.data.code == 200) {
        if (response.data.data && response.data.data.length > 0) {
          var res = response.data.data;
          self.setData({
            carpoolTypeList: res,
            currentTypeId: res[0].id
          })
          self.getCarpoolList(res[0].id);
        }
      }
    })
  },

  //获取拼车信息
  getCarpoolList: function (id){
    wx.showLoading({
      title: '正在加载',
      mask: true
    });  
    var self = this;
    var data = {
      category_id: id,
      page: self.data.page,
      order: '-create_time',
      limit: pageCount
    };
    var getCarpoolListRequest = wxRequest.getRequest(Api.getCarpoolList(),data);
    getCarpoolListRequest.then(response => {
      if (response.statusCode == 200 && response.data.code == 200) {
        if (response.data.data && response.data.data.length > 0) {
          if (response.data.data.length < pageCount) {
            self.setData({
              isLastPage: true
            });
          }
          self.setData({
            carpoolList: self.data.carpoolList.concat(response.data.data.map(function (item) {
              var strdate = item.departure_date;
              var strtime = item.departure_time;
              item.dateStr = util.getDateDiffByCarpool(strdate, strtime);
              return item;
            }))
          })
        }
      }
      wx.hideLoading();
    })    
  },

  //切换顺风车类型
  swichNav: function (e) {
    var that = this;
    var current_idx = e.currentTarget.dataset.current;
    var current_type_id = e.currentTarget.dataset.id;
    if (that.data.currentNav === current_idx) {
      return false;
    } else {
      that.setData({
        currentNav: current_idx,
        carpoolList: [],
        currentTypeId: current_type_id,
        isLastPage: false,
        page: 1
      });
      that.getCarpoolList(current_type_id);
    }
  },
  /**
  * 联系他
  */
  tap_phone: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({ phoneNumber: phone });
  },
  //加载分页
  loadMore: function (e) {
    var self = this;
    if (!self.data.isLastPage) {
      self.setData({
        page: self.data.page + 1
      });
      self.getCarpoolList(self.data.currentTypeId);
    }
  },
  //底部加载
  onReachBottom: function () {
    var self = this;
    self.loadMore();
  }
})